#!/bin/sh
#
# @license   http://www.gnu.org/licenses/gpl.html GPL Version 3
# @author    Ian Moore <imooreyahoo@gmail.com>
# @copyright Copyright (c) 2011 Ian Moore
#
# This file is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this file. If not, see <http://www.gnu.org/licenses/>.

set -e

. /etc/default/openmediavault
. /usr/share/openmediavault/scripts/helper-functions

OMV_AVAHIDAEMON_MYSQL=${OMV_AVAHIDAEMON_MYSQL:-"/etc/avahi/services/mysql.service"}
OMV_AVAHIDAEMON_MYSQL_NAME=${OMV_AVAHIDAEMON_MYSQL_NAME:-"%h - MySQL"}
OMV_AVAHIDAEMON_MYSQL_PORT=$(omv_config_get "//services/mysql/port")

# Is the service enabled?
rm -f ${OMV_AVAHIDAEMON_MYSQL}
[ "$(omv_config_get "//services/mysql/enable")" = "0" ] && exit 0
[ "$(omv_config_get "//services/mysql/enable-networking")" = "0" ] && exit 0

# Create '/etc/avahi/services/mysql.service' file.
cat <<EOF > ${OMV_AVAHIDAEMON_MYSQL}
<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
  <name replace-wildcards="yes">${OMV_AVAHIDAEMON_MYSQL_NAME}</name>
  <service>
    <type>_mysql._tcp</type>
    <port>${OMV_AVAHIDAEMON_MYSQL_PORT}</port>
  </service>
</service-group>
EOF
